//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lyq.bean.service;

import com.lyq.bean.Course;
import com.lyq.bean.Student;
import com.lyq.bean.Elective;
import com.lyq.bean.mySql.*;

import java.util.ArrayList;
import java.util.List;

public class E_Service {
    public E_Service() {
    }
    
    public String elect_C(String cNo, String sNo, String name) {
        List<Course> list = new ArrayList<Course>();
        String cname = null;
        list = (new C_Service()).seek_C("编号", "精确查询", cNo, list);
        
        if (list.size() > 0) {
            cname = ((Course) list.toArray()[0]).getName();
        }
        
        String sname = null;
        List<Student> list2 = new ArrayList<Student>();
        list2 = (new S_Service()).seek_S("学号", "精确查询", sNo, list2);
        
        if (list2.size() > 0) {
            sname = ((Student) list2.toArray()[0]).getName();
        }
        
        Boolean bool = (new SeekElected_Sql()).seekExist_Sql(cNo, sNo);
        if (bool) {
            return "该课已选，请选其它课";
        }
        
        Boolean bool2 = (new MinusResidueNum_Sql()).seekResidueNum_Sql(cNo);
        if (!bool2) {
            return "该班学生已满，请选其它课";
        }
        
        bool = (new ElectC_Sql()).electC_Sql(cNo, cname, sNo);
        if (bool) {
            return "选课成功";
        } else {
            return "选课失败";
        }
    }
    
    public List<Course> seek_ElectC(String sNo) {
        List<Course> list = new ArrayList<Course>();
        // 获取学生已选课程的完整信息
        list = (new SeekElectC_Sql()).seekElectC_SqlWithFullInfo(sNo, list);
        return list;
    }
    
    public Boolean recede_C(String cNo, String sNo) {
        // 先增加课程剩余人数
        Boolean bool = (new PlusResidueNum_Sql()).plusResidueNum_Sql(cNo);
        if (bool) {
            // 再从选课表中删除选课记录
            bool = (new RecedeC_Sql()).recedeC_Sql(cNo, sNo);
            // 如果删除选课记录失败，则需要回滚，将课程剩余人数减1
            if (!bool) {
                (new MinusResidueNum_Sql()).seekResidueNum_Sql(cNo);
            }
        }
        return bool;
    }
    
    public List<Elective> seekScore(String sNo, List<Elective> list) {
        List<Elective> list2 = (new SeekScore_Sql()).seekScore_Sql(sNo);
        return list2;
    }
    
    // 检查课程是否已选
    public boolean isCourseElected(String cNo, String sNo) {
        Boolean bool = (new SeekElected_Sql()).seekExist_Sql(cNo, sNo);
        return bool;
    }
    
    // 检查时间冲突
    public boolean hasTimeConflict(String newCourseNo, String studentNo) {
        // 获取学生已选课程
        List<Course> electedCourses = this.seek_ElectC(studentNo);
        
        // 获取所有课程信息
        List<Course> allCourses = new ArrayList<>();
        allCourses = (new C_Service()).seekAll_C(allCourses);
        
        // 找到新选课程的时间
        String newCourseTime = "";
        for (Course course : allCourses) {
            if (course.getNo().equals(newCourseNo)) {
                newCourseTime = course.getTime();
                break;
            }
        }
        
        // 检查与已选课程的时间是否冲突
        for (Course electedCourse : electedCourses) {
            String electedCourseNo = electedCourse.getNo();
            // 跳过自身
            if (electedCourseNo.equals(newCourseNo)) {
                continue;
            }
            
            // 获取已选课程的时间
            for (Course course : allCourses) {
                if (course.getNo().equals(electedCourseNo)) {
                    String electedCourseTime = course.getTime();
                    // 简单的时间冲突检测（实际应用中需要更复杂的逻辑）
                    if (newCourseTime.equals(electedCourseTime)) {
                        return true; // 发现时间冲突
                    }
                    break;
                }
            }
        }
        
        return false; // 没有时间冲突
    }
}